package com.ncu.musicbox2020_2;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Binder;
import android.os.IBinder;
import android.provider.MediaStore;
import android.widget.RemoteViews;

import androidx.core.app.NotificationCompat;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MusicService extends Service {

    private List<MusicInfo> musiclist;
    private Random random;
    private MediaPlayer mPlayer;
    private Context context;
    private int seekLength = 0;
    private int currentIndex = -1;
    private int total_music;

    public MusicService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return new MyBinder();
    }
    @Override
    public int  onStartCommand(Intent intent, int flags, int startId) {

        return super.onStartCommand(intent,flags,startId);
    }
    @Override
    public void onCreate(){
        this.context = context;
        random = new Random(System.currentTimeMillis());
        initPlayer();
        //通过工具类MusicUtils获取音乐信息列表
        musiclist = MusicUtils.ResolveMusicToList(getApplicationContext());

        Intent pp = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getService(this, 1, pp, PendingIntent.FLAG_CANCEL_CURRENT);
        Notification compatbuilder = new NotificationCompat.Builder(this)
                .setContentText("Music")
                .setSmallIcon(R.drawable.music)
                .setWhen(System.currentTimeMillis())
                .setContentIntent(pendingIntent)
                .build();
        compatbuilder.contentIntent = pendingIntent;
        compatbuilder.flags = Notification.FLAG_SHOW_LIGHTS;
        startForeground(1,compatbuilder);
    }

    public void initPlayer(){
        mPlayer = new MediaPlayer();
        mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    }


    //bind绑定类，用来专门放函数
    private class MyBinder extends Binder implements MyBinderInterface{
        public void Release(){//释放
            mPlayer.reset();
            mPlayer.stop();
            mPlayer.release();
        }
        public void Pause(){//暂停
            if(mPlayer.isPlaying()){
                mPlayer.pause();
                seekLength = mPlayer.getCurrentPosition();//获得当前的位置并且为这个实例储存下来
            }
        }
        public void Resume(){//恢复
            //修改定位位置
            mPlayer.seekTo(seekLength);
            mPlayer.start();
        }
        public void SetSeekPos(int seekPos){//修改播放位置
            mPlayer.seekTo(seekPos);
        }
        public void Reset(){//播放复位
            seekLength = 0;
            mPlayer.seekTo(seekLength);
        }
        public void Play(){//播放
            mPlayer.reset();
            Uri path = Uri.parse(musiclist.get(currentIndex).getMusic_path());
            try{
                mPlayer.setDataSource(String.valueOf(path));
                mPlayer.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
            mPlayer.seekTo(seekLength);
            mPlayer.start();
        }
        //顺序
        public void PlayNext(){
            currentIndex = currentIndex + 1;
            if(currentIndex>musiclist.size()){
                currentIndex = 0;
            }
            seekLength = 0;
            if(mPlayer.isPlaying()){
                Play();
            }
        }
        public void PlayPrev(){
            currentIndex = currentIndex - 1;
            if(currentIndex<0){
                currentIndex = musiclist.size()-1;
            }
            seekLength = 0;
            if(mPlayer.isPlaying()){
                Play();
            }
        }
        //单曲
        public void PlayThis(){
            //播放的还是这个音乐
            currentIndex = currentIndex;
            seekLength = 0;
            if(mPlayer.isPlaying()){
                Play();
            }
        }
        //随机
        public void PlayRandom(){
            Random random = new Random();
            int rand = random.nextInt(total_music);
            currentIndex = rand;
            if(currentIndex>musiclist.size()){
                currentIndex = 0;
            }
            seekLength = 0;
            if(mPlayer.isPlaying()){
                Play();
            }
        }
        public boolean isPlaying(){
            return mPlayer.isPlaying();
        }
        public int getDuration(){
            return mPlayer.getDuration();
        }
        public int getCurrentPosition(){
            return mPlayer.getCurrentPosition();
        }
        public void seekTo(int length){
            seekLength = length;
            mPlayer.seekTo(length);
        }
        public void setCurrentIndex(int index){
            currentIndex = index;
        }
        public int getCurrentIndex(){
            return currentIndex;
        }
        MusicInfo getMusicInfo(int index){
            return musiclist.get(index);
        }
        public List<MusicInfo> getMusiclist(){
            return musiclist;
        }
        public int getTotal_music(){
            return musiclist.size();
        }
    }
}
